
/*
This file is part of MJIN2.

Copyright (C) 2012-2013 Opensource Game Studio

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#ifndef MJIN2_CONFIG_FILE_H
#define MJIN2_CONFIG_FILE_H

#include <mjin2/Common.h>
#include <mjin2/Exception.h>

namespace mjin2
{
namespace priv
{
class ConfigFile;
}

//! INI format configuration file reader / writer.
/*! Example:
 *  \code
 *  ConfigFile cfg("~/.config/app.cfg");
 *  String resolution = cfg.get("Resolution", "1024 x 768");
 *  . . .
 *  cfg.set("Resolution", resolution);
 *  cfg.save();
 *  \endcode
 *  The example simply loads INI formatted ~/.config/app.cfg file,
 *  does something, and then saves the file.
 */
class ConfigFile
{
        DISALLOW_COPY_AND_ASSIGN(ConfigFile);
    public:
        ConfigFile() throw ();
        //! Invokes loadAndCreate().
        ConfigFile(const String &fileName) throw (Exception);
        ~ConfigFile() throw ();

        //! Erase in-memory data.
        void clear() throw ();
        bool exists(const String &key) const throw ();
        //! Get key value.

        //! Getting key that does not exist without defaultValue results
        //! in exception.
        //! defaultValue is used when no value for the key exists, the
        //! key value is assigned to defaultValue in this case.
        String get(const String &key,
                   const String &defaultValue = "") throw (Exception);
        //! Load the file. Create it if it doesn't exist.
        void loadAndCreate(const String &fileName) throw (Exception);
        void save(const String &fileName) throw (Exception);
        void set(const String &key, const String &value) throw (Exception);
        //! Get contents of read config file.
        String str() const throw ();

    private:
        priv::ConfigFile *mD;
};

} // namespace mjin2

#endif // MJIN2_CONFIG_FILE_H

